
local excel = load_excel('商店设置.xlsx','奇遇事件')

local weights = {}
for i,data in ipairs(excel) do
    local name = data['名称']
    data.name = name
    data.skill_class = '奇遇事件'
    data.tip = data['属性类型']
    ac.lni('skill',name,data)
    weights[i] = {name,data['权重'] or 1}
end

local qy_model1 = [[QY_csm.mdl]]
local create_qy_unit = function(player,name,jy,num,count,func)
    local point = player:getAtkPoint()
    point:add_effect(qy_model1,count/num)
    ac.loop(1000/num,function(t)
        count = count - 1
        if count<0 then
            local enemy = player:create_enemy(jy,point)
            enemy:add_type('精英')
            func(enemy,true)
            t:remove()
            return
        end
        local enemy = player:create_enemy(name,point)
        func(enemy)
    end)
end


ac.game:event '单位-发动技能效果'(function(_,unit,skill)
    if skill.skill_class=='奇遇事件' then
        local player = unit.owner
        local hero = player.hero
        local name = skill.name
        local tip = ('[%s]%s'):format(skill.name,skill.tip)
        player:send_msg(tip)
        pz(skill.title or skill.name,hero:get_point() - {0,150})
        unit:remove()
        if name=='斩怪杀敌' then
            create_qy_unit(player,'奇遇杀敌','杀敌精英',2,50,function(unit,jy)
                if jy then
                    unit['杀敌'] = 500
                else
                    unit['杀敌'] = math.random(6,10)
                end
            end)
        elseif name=='木之精灵' then
            create_qy_unit(player,'奇遇木材','木材精英',1,30,function(unit,jy)
                if jy then
                    unit['木材'] = 100
                else
                    unit['木材'] = math.random(1,10)
                end
            end)
        elseif name=='灵气之源' then
            create_qy_unit(player,'蜘蛛','蜘蛛精英',1,10,function(unit,jy)
                unit:event '单位-被英雄击杀'(function()
                    if jy then
                        local index = math.random(3)
                        local keys = {'力量加成','敏捷加成','智力加成'}
                        local text = {'|cffff0000力量加成+3%|r','|cff00ff00敏捷加成+3%|r','|cff00ccff敏捷加成+3%|r'}
                        local key = table.random
                        hero:add(keys[index],3)
                        pz(text[index],hero:get_point() - {0,150})
                    else
                        local index = math.random(3)
                        local keys = {'基础力量','基础敏捷','基础智力'}
                        local text = {'|cffff0000力量+30|r','|cff00ff00敏捷+30|r','|cff00ccff智力+30|r'}
                        hero:add(keys[index],30)
                        pz(text[index],hero:get_point() - {0,150})
                    end
                end)
            end)
        elseif name=='经验宝宝' then
            create_qy_unit(player,'鱼人','鱼人精英',1,10,function(unit,jy)
                unit:event '单位-被英雄击杀'(function()
                    if jy then
                        hero:add_level(1)
                    else
                        hero:add_exp(100)
                    end
                end)
            end)
        elseif name=='夺宝小兵' then
            local enemy = player:create_enemy('宝箱怪')
            enemy.title = '夺宝小兵'
            enemy:set('移动速度',100)
            enemy:add_restriction '幽灵'
            enemy:add_buff '挑战限时'{time = 20}
            enemy:event '单位-受到伤害前'(function(_,_,_,data)
                if data.skill==nil then
                    player:notify('玩家-宝箱奖励',player)
                end
                data:div(100)
            end)
        elseif name=='异界之门' then
            local items = {'六级吞噬丹','转生之源','觉醒石'}
            create_qy_unit(player,'女妖','异界幽魂',1,10,function(unit,jy)
                unit:event '单位-被英雄击杀'(function()
                    if jy then
                        local name = table.random(items)
                        if name=='觉醒石' then
                            local item = hero:create_jx_item()
                            player:send_msg(('获得:%s'):format(item:get_title()))
                        else
                            local item = collate_item(hero,name)
                            player:send_msg(('获得:%s'):format(item:get_title()))
                        end
                    else
                        player:add_icon('金币',500)
                        player:add_icon('木材',20)
                        player:add_icon('杀敌',100)
                    end
                end)
            end)
        elseif name=='山丘之王' then
            local enemy = player:create_enemy('山丘之王')
            enemy:set_size(2)
        elseif name=='怪物魔化' then
            local tir = player:event '玩家-创建敌人'(function(_,_,unit)
                if unit.name=='骷髅战士' then
                    unit:add('攻击加成',50)
                    unit:add('生命加成',50)
                    unit:add_effect([[Abilities\Spells\Undead\UnholyFrenzy\UnholyFrenzyTarget.mdl]],'overhead')
                    unit['金币'] = (unit['金币'] or 0) + 10
                    unit['经验'] = (unit['经验'] or 0) + 10
                end
            end)
            ac.wait(20*1000,function()
                tir:remove()
            end)
        elseif name=='攻速强化' then
            collate_item(hero,'攻速药剂')
        elseif name=='护甲强化' then
            collate_item(hero,'治愈药剂')
        elseif name=='铸造大师' then
            local item = hero:add_item('重铸宝石',{count = 2})
            collate_item(hero,item)
        elseif name=='战神附身' then
            hero:add_buff '临时属性'{
                time = 20,
                art = skill.art,
                title = skill.title,
                tip = [[暴击/冷却获得提升]],
                sx = {
                    ['暴击几率'] = 30,
                    ['冷却速度'] = 50,
                    ['多重数量'] = 5,
                }
            }
        elseif name=='魔法源泉' then
            collate_item(hero,'魔法药剂')
        elseif name=='战意狂涌' then
            hero:add_buff '临时属性'{
                time = 20,
                art = skill.art,
                title = skill.title,
                tip = [[最终伤害获得提升]],
                sx = {
                    ['最终伤害'] = 20,
                }
            }
        elseif name=='命运抉择' then
            hero:notify('单位-添加升级奖励',hero,2)
        elseif name=='属性转换' then
            collate_item(hero,'属性转换宝石')
        elseif name=='宝箱挖掘' then
            for a=1,4 do
                player:create_enemy('宝箱')
            end
        elseif name=='英雄卡牌' then
            hero:create_jx_item()
        elseif name=='冲击波' then
            hero:add_buff '冲击波'{
                time = 20,
                keep = 1,
                art = skill.art,
                title = skill.title,
                tip = [[持续发射冲击波]],
                pulse = 0.2,
                on_pulse = function()
                    local angle = math.random(0,360)
                    local mover = hero:launch_wave([[Abilities\Spells\Orc\Shockwave\ShockwaveMissile.mdl]],angle,1200,1200)
                    local damage = math.max(hero:get('力量'),hero:get('敏捷'),hero:get('智力')) * 2
                    sc.loopUnit(0.03,mover,120,function(group,unit)
                        if unit:is_enemy(hero) then
                            hero:create_damage(unit,damage)
                        end
                    end)
                end
            }
        elseif name=='死神收割' then
            collate_item(hero,'死神药剂')
        elseif name=='属性加持' then
            hero:add_buff '临时属性'{
                time = 20,
                art = skill.art,
                title = skill.title,
                tip = [[全属性提升]],
                sx = {
                    ['力量加成'] = 40,
                    ['敏捷加成'] = 40,
                    ['智力加成'] = 40,
                }
            }
        end
    end
end)

local mt = ac.skill['随机选择事件']
mt.tip = [[随机选择一个奇遇事件发动]]
function mt:on_cast_shot()
    local unit = self.owner
    local player = unit.owner
    local hero = player.hero
    local name = table.get_by_weight(weights)
    local skill = unit:add_skill(name,'英雄')
    skill:cast()
end


ac.game:event '英雄-刷新随机事件'(function(_,player,test)
    local npc = player:get_data('奇遇NPC')
    if npc then
        npc:remove()
    end
    local point = player.shop_point1:random(512)
    local unit = player:create_unit('奇遇事件',point,270)
    local name = table.get_by_weight(weights)
    unit:add_effect([[GN_gthts.mdl]])
    unit.lock_event = name
    
    unit_add_life2(unit)
    unit:set_data('血条贴图',[[core\hpbar\黄条.tga]])
    unit:set('生命上限',60.1)
    unit:add('生命恢复',-1)
    unit:add_buff '挑战限时'{time = 60}
    player:set_data('奇遇NPC',unit)
    player:notify('UI-刷新动画',player,unit)
    
    if test then
        for a=1,10 do
            local id = (test-1)*10 + a
            local data = weights[id]
            if data then
                local name = data[1]
                unit:add_skill(name,'英雄')
            end
        end
        unit:add_skill('随机选择事件','英雄')
    else
        unit:add_skill(name,'英雄',1)
        unit:add_skill('随机选择事件','英雄',2)
    end
end)


ac.game:event '英雄-击杀单位'(function(_,hero,unit)
    local count = hero:get_data('奇遇击杀',0) + 1
    local player = hero.owner
    if count==1000*(1 - player:get_data('奇遇效率',0)/100) then
        count = 0
        player:notify('英雄-刷新随机事件',player)
    end
    hero:set_data('奇遇击杀',count)
end)



ac.item('攻速药剂'){
    item_type = "charged",
    auto_merge = true,
    model = [[wp_yjmx.mdl]],
    art = [[图标\wp_gsyj.blp]],
    color = 0xffff6800,
    scale = 1.5,
    tip = [[使用后攻击速度+200%，攻击间隔-0.1，持续30秒]],
    on_use = function(self)
        local unit = self.owner
        local player = unit.owner
        local hero = player.hero
        hero:add_buff '临时属性'{
            time = 30,
            art = self.art,
            title = self.title,
            tip = [[攻速/间隔获得提升]],
            sx = {
                ['攻击速度'] = 200,
                ['攻击间隔'] = -0.1,
            },
        }
    end,
}


ac.item('治愈药剂'){
    temp = '攻速药剂',
    art = [[图标\wp_zyyj.blp]],
    tip = [[使用后立即回复20%最大生命值，之后每秒恢复10%最大生命值，持续20秒]],
    color = 0xff00ff00,
    on_use = function(self)
        local unit = self.owner
        local player = unit.owner
        local hero = player.hero
        hero:add('生命',hero:get('生命上限')*0.2)
        hero:add_buff '临时属性'{
            time = 20,
            art = self.art,
            title = self.title,
            tip = [[生命恢复加快]],
            sx = {
                ['生命百分比恢复'] = 10,
            }
        }
    end,
}

ac.item('魔法药剂'){
    temp = '攻速药剂',
    art = [[图标\wp_mfyj.blp]],
    tip = [[使用后每秒回蓝+50，最终伤害+20%，持续20S。选择后永久获得全属性+1%]],
    color = 0xffca42fd,
    on_use = function(self)
        local unit = self.owner
        local player = unit.owner
        local hero = player.hero
        hero:add('全属性加成',1)
        hero:add_buff '临时属性'{
            time = 20,
            art = self.art,
            title = self.title,
            tip = [[魔法恢复加快]],
            sx = {
                ['魔法恢复'] = 50,
                ['最终伤害'] = 20,
            }
        }
    end,
}

ac.item('死神药剂'){
    temp = '攻速药剂',
    art = [[图标\wp_ssyj.blp]],
    tip = [[使用后杀敌全属性+0.5，提高40%全属性加成，持续20秒]],
    color = 0xffff0000,
    on_use = function(self)
        local unit = self.owner
        local player = unit.owner
        local hero = player.hero
        hero:add_buff '临时属性'{
            time = 20,
            art = self.art,
            title = self.title,
            tip = [[杀敌全属性提升]],
            sx = {
                ['杀敌力量'] = 0.5,
                ['杀敌敏捷'] = 0.5,
                ['杀敌智力'] = 0.5,
                ['全属性加成'] = 40,
            }
        }
    end,
}














